var express = require('express');
var router = express.Router();
const User = require('../model/user'); // 引入数据模型
require('./../utils/dateFormat');
var path = require('path');
var avatar = path.join(__dirname, '../assets/avatar.jpg');
// 注册
router.get('/assets/avatar.jpg', (req, res) => {
  res.sendFile(avatar)
})
router.post('/register', async (req, res) => {
  let {
    userPhone,
    userName,
    userPassword
  } = req.body;
  try {
    if (!userPhone || !userName || !userPassword) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      const exist = await User.findOne({
        userPhone
      })
      if (exist) {
        res.json({
          resultCode: '0001',
          resultMessage: '账号已存在',
          result: ''
        })
      } else {
        let r1 = Math.floor(Math.random() * 10);
        let r2 = Math.floor(Math.random() * 10);
        let userId = `${r1}${(Date.parse(new Date())) / 1000}${r2}`
        // 注册
        User.insertMany({
          userAvatar: 'http://localhost:8080/api/users/assets/avatar.jpg',
          userName: userName,
          userPhone: userPhone,
          userPassword: userPassword,
          userId: userId,
          userCreate: new Date().Format('yyyy-MM-dd hh:mm:ss')
        })
        res.json({
          resultCode: '0000',
          resultMessage: '注册成功',
          result: ''
        })
      }
    }
  } catch (err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 登录
router.post('/login', async (req, res) => {
  let { userPhone, userPassword } = req.body;
  try {
    if (!userPhone || !userPassword) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误'
      })
    } else {
      const exist = await User.findOne({
        userPhone
      })
      if (exist) {
        const { userId } = exist;
        res.cookie('userId', userId, {
          path: '/login',
          maxAge: 1000 * 60 *60
        });
        // req.session.userId = userId;
        res.json({
          resultCode: '0000',
          resultMessage: '登录成功',
          result: exist
        })
      } else {
        res.json({
          resultCode: '0003',
          resultMessage: '账号未注册',
          result: ''
        })
      }
    }
  } catch (err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 获取用户详细信息
router.post('/getUser', async (req, res) => {
  let { userId } = req.body;
  try {
    if (!userId) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      const userInfo = await User.findOne({ userId })
      if (userInfo) {
        res.json({
          resultCode: '0000',
          resultMessage: '获取成功',
          result: userInfo
        })
      } else {
        res.json({
          resultCode: '0003',
          resultMessage: '无个人信息',
          result: ''
        })
      }
    }
  } catch(err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 用户退出
router.post('/logOut', (req, res) => {
  try {
    res.clearCookie('userId')
    res.json({
      resultCode: '0000',
      resultMessage: '退出成功',
      result: ''
    })
  } catch(err) {
    res.json({
      resultCode: '9999',
      resultMessage: '系统错误',
      result: ''
    })
  }
})
/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;